Lists and Choices


In [4]:
odds = [1,3,5,7]
print('Odds are:', odds)


Odds are: [1, 3, 5, 7]

In [8]:
print(odds[0], odds[-1])


1 7

In [9]:
for number in odds:
    print(number)


1
3
5
7

In [11]:
print(odds[1])
print(odds)


3
[1, 3, 5, 7]

In [12]:
odds[1] = 11
print(odds)


[1, 11, 5, 7]

In [13]:
string = 'oxygen'
print(string[1])


x

In [14]:
string[1] = i
print(string)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-14-c087aeca6c3a> in <module>()
----> 1 string[1] = i
      2 print(string)

NameError: name 'i' is not defined

In [23]:
names = ['Newton', 'Darwing', 'Turing']
print('Names list is currently:', names)
names[1] = 'Darwin'
print('Names list is now:', names)


Names list is currently: ['Newton', 'Darwing', 'Turing']
Names list is now: ['Newton', 'Darwin', 'Turing']

In [21]:
names[1] = 'Darwin'

names


In [22]:
names


Out[22]:
['Newton', 'Darwin', 'Turing']

In [24]:
name = 'Bell'
name[0] = 'b'


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-24-220df48aeb2e> in <module>()
      1 name = 'Bell'
----> 2 name[0] = 'b'

TypeError: 'str' object does not support item assignment

In [25]:
x = [['pepper', 'zucchini', 'onion'],['apple', 'pear', 'banana']]

In [26]:
x


Out[26]:
[['pepper', 'zucchini', 'onion'], ['apple', 'pear', 'banana']]

In [27]:
print(x)


[['pepper', 'zucchini', 'onion'], ['apple', 'pear', 'banana']]

In [28]:
for list in x:
    print(list)


['pepper', 'zucchini', 'onion']
['apple', 'pear', 'banana']

In [36]:
# Our master list
print(x)

# Find first thing in first list
print(x[0][0])

# Find first letter of first thing in first list
print(x[0][0][0])


[['pepper', 'zucchini', 'onion'], ['apple', 'pear', 'banana']]
pepper
p

In [35]:
# Find first letter of first thing in first list
x[0][0][0]


Out[35]:
'p'

In [37]:
55
100


Out[37]:
100

In [38]:
print(odds)


[1, 11, 5, 7]

In [41]:
odds.append(13)
print('odds list after adding a number:', odds)


odds list after adding a number: [1, 11, 5, 7, 13, 13, 13]

In [44]:
del odds[-1]
print(odds)


[1, 11, 5, 7, 13]

In [48]:
odds.reverse()
print(odds)


[13, 7, 5, 11, 1]

In [46]:
odds.reverse()


Out[46]:
[13, 7, 5, 11, 1]

String into a list


In [53]:
string = 'hello'
new_list = []
for x in string:
    new_list.append(x)
    print(new_list)
print(new_list)


['h']
['h', 'e']
['h', 'e', 'l']
['h', 'e', 'l', 'l']
['h', 'e', 'l', 'l', 'o']
['h', 'e', 'l', 'l', 'o']

Conditionals!


In [60]:
num = 101

if num > 100:
    print('Greater!')
else: 
    print('Lesser!')
print('All done')


Greater!
All done

In [68]:
num = 1000

if num > 0:
    print(num, 'is positive')
elif num == 0:
    print(num, 'is zero')
else:
    print(num, 'is negative')


1000 is positive

In [70]:
if (1 > 0) and (-1 >0): 
    print('True')
else: 
    print('False')


False

In [73]:
if (1 > 0) or (-1 >0): 
    print('... at least one thing is True')
else: 
    print('False')


... at least one thing is True

What is Truth?


In [79]:
if '':
    print("empty string is true")
if 'word':
    print("some string is true")
if [1,2,3]:
    print('some list is true')
if 0:
    print('zero is true')
if 1:
    print('one is true')


some string is true
some list is true
one is true

In [92]:
a = 100
b = 100
if a > 0.9*b:
    print('TRUE')
elif a < 1.1*b:
    print('TRUE')


TRUE

In [93]:
x = 10

In [94]:
x = x+2
x


Out[94]:
12

In [96]:
x += 2
x


Out[96]:
16

Functions


In [4]:
def fahr_to_kelvin(temp):
    newtemp = ((temp - 32)*(5/9)) + 273.15
    return newtemp

In [6]:
res = fahr_to_kelvin(0)
print (res)


255.3722222222222

In [7]:
x = 11
print(x)
x = float(x)
print(type(x))


11
<class 'float'>

In [8]:
5/9


Out[8]:
0.5555555555555556

In [9]:
def kelvin_to_celcius(temp_k):
    converted = temp_k - 273.15
    return converted

In [12]:
kelvin_to_celcius(0)


Out[12]:
-273.15

In [13]:
def fahr_to_celcius(temp_f):
    temp_k = fahr_to_kelvin(temp_f)
    result = kelvin_to_celcius(temp_k)
    return result

In [16]:
print('freezing point of water in celsius', fahr_to_celcius(32))


freezing point of water in celsius 0.0

In [17]:
a = 'Hi'
b = ' everyone'
a + b


Out[17]:
'Hi everyone'

In [32]:
def join_words(word1, word2):
    join = word1 + word2
    return join

In [34]:
join_words('My name is ', 'Jack')


Out[34]:
'My name is Jack'

In [51]:
def abbrev(outer):
    return outer [0] + outer [-1]

In [52]:
outer = ('helium')

In [53]:
abbrev(outer)


Out[53]:
'hm'

In [61]:
my_numbers = [1,2,3,4,5,6,7,8,9,10]

def sum_numbers(numbers_to_multiply):
    new_numbers = []
    for x in my_numbers:
        result = numbers_to_multiply*x
        new_numbers.append(result)
        
    return new_numbers

In [62]:
sum_numbers(5)


Out[62]:
[5, 10, 15, 20, 25, 30, 35, 40, 45, 50]

sum_numbers(2)


In [63]:
sum_numbers(2)


Out[63]:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

In [64]:
my_numbers = [1,2,3,4,5,6,7,8,9,10]

def sum_numbers(numbers_to_multiply):
    new_numbers = []
    for x in my_numbers:
        new_numbers.append(numbers_to_multiply*x)
        
    return new_numbers

In [65]:
sum_numbers(2)


Out[65]:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

In [ ]: